<template>
  <div id="bg">
    <div id="el-login">
      <h1>欢迎来到尔玉管理平台</h1>
      <img src="" style="width: 60px" alt="">
      <h2>请先登录账号</h2>
      <el-tabs type="border-card"
               style="width: 400px;
                      height: 300px;
                      margin: 0 auto;
                      background-color:rgba(255,255,255,0.3)">
        <el-tab-pane label="账号登录">
          <!--            label-width设置每个表单项 文本标签的宽度-->
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  style="width: 300px;margin: 20px auto" label-width="60px">
            <el-form-item label="用户名" prop="username" style="color: black;margin-bottom: 20px">
              <el-input type="text" v-model="ruleForm.username" placeholder="请输入用户名" ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" style="color: black">
              <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button style="position:relative;right: 30px" type="primary" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="手机号登录">
          <el-form style="width: 300px;margin: 20px auto" label-width="70px">
            <el-form-item label="手机号码" style="color: black;margin-bottom: 20px">
              <el-input type="text" placeholder="请输入11位手机号"></el-input>
            </el-form-item>
            <el-form-item label="验证码" style="color: black;width: 160px">
              <el-input type="password" placeholder="请输入验证码"></el-input>
              <el-button style="display: inline-block" type="primary">获取验证码</el-button>
            </el-form-item>
            <el-form-item>
              <el-button style="position: relative;right: 30px" type="primary" >登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let url = 'http://localhost:9080/admins/login';
          console.log('url >>> ' + url);
          let data = {
            'username': this.ruleForm.username,
            'password': this.ruleForm.password
          };
          console.log('data >>> ');
          console.log(data);
          this.axios.post(url, data).then((response) => {
            console.log(response.data.code);
            if (response.data.code == 20000) {
              // 提示登录成功
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              // 获取服务器端响应的JWT并存入到LocalStorage中
              let jwt = response.data.data;
              console.log(jwt);
              localStorage.setItem('jwt', jwt);
              // 跳转到主页
              this.$router.push('/admin');
            } else {
              this.$message.error('登录失败，用户名或密码错误！');
            }
          }).catch(function () {
            console.log('响应结果为失败！');
          })
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
#bg {
  background-repeat: no-repeat;
  background-image: url("/src/views/admin/bg01.png");
  background-size: 70%;
  background-position: center;
  background-color: black;
  margin: 0;
  overflow: hidden;
}

#el-login {
  color: #3a8ee6;
  width: 500px;
  height: 595px;
  margin-left: 950px;
  margin-top: 100px;

}

</style>
